﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplaceJt.CCoTcReplaceJtCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Utilities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.TcCommands.TcReplaceJt;

public class CCoTcReplaceJtCmd : TxButtonCommand
{
  protected CCoTcReplaceJtCmdEnabler m_cmdEnabler;

  public CCoTcReplaceJtCmd() => this.m_cmdEnabler = new CCoTcReplaceJtCmdEnabler();

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_cmdEnabler;

  public virtual string Name => TcReplaceJtStringTable.CMD_NAME;

  public virtual string Category => TcReplaceJtStringTable.CMD_CATEGORY;

  public virtual string Tooltip => TcReplaceJtStringTable.CMD_TOOLTIP;

  public virtual string Description => TcReplaceJtStringTable.CMD_DESCRIPTION;

  public virtual string[] AdvancedDescriptions => TxCallMessages.CollectMessages((ITxCommand) this);

  public virtual string LargeBitmap => "TcCommands.TcReplaceJt.Images.ReplaceJt_32.png";

  public virtual string Bitmap => "TcCommands.TcReplaceJt.Images.ReplaceJt_16.bmp";

  public virtual string MediumBitmap => "TcCommands.TcReplaceJt.Images.ReplaceJt_24.png";

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("SetReplaceJt");

  public virtual void Execute(object cmdParams)
  {
    CApTcReplaceJtModel initialModel = this.CreateInitialModel();
    if (initialModel == null)
      return;
    bool flag = this.ValidateInitialModel(initialModel);
    if (flag)
    {
      bool? nullable = new CUiTcReplaceJt(initialModel).ShowDialog(true);
      flag = nullable.HasValue && nullable.Value;
    }
    bool instanceIsInModelling = initialModel.SelectedInstanceIsInModelling;
    if (!flag || !instanceIsInModelling && !this.PromptSaveOrUpdateTcAndProceed())
      return;
    this.ExecuteReplace(initialModel);
  }

  private CApTcReplaceJtModel CreateInitialModel()
  {
    ITxPlanningInstance selectedInstance = (ITxPlanningInstance) null;
    TxObjectList planningItems = TxApplication.ActiveSelection.GetPlanningItems();
    if (((Collection<ITxObject>) planningItems).Count > 0)
      selectedInstance = ((Collection<ITxObject>) planningItems)[0] as ITxPlanningInstance;
    CApTcReplaceJtModel initialModel = (CApTcReplaceJtModel) null;
    if (selectedInstance != null)
      initialModel = new CApTcReplaceJtModel(selectedInstance);
    return initialModel;
  }

  private bool IsReplaceMode(CApTcReplaceJtModel model) => model.DoesHaveJT;

  private bool ValidateInitialModel(CApTcReplaceJtModel model)
  {
    bool flag = true;
    if (flag)
      flag = this.ValidateNoOtherInstancesInModeling(model);
    return flag;
  }

  private bool ValidateNoOtherInstancesInModeling(CApTcReplaceJtModel model)
  {
    bool flag = true;
    TxObjectList<ITxPlanningInstance> instancesInModeling = model.GetAllOtherInstancesInModeling();
    if (((Collection<ITxPlanningInstance>) instancesInModeling).Count > 0)
    {
      flag = false;
      List<string> stringList = new List<string>();
      foreach (ITxPlanningInstance planningInstance in (Collection<ITxPlanningInstance>) instancesInModeling)
        stringList.Add(((ITxObject) planningInstance).Name);
      stringList.Sort();
      string str1 = !this.IsReplaceMode(model) ? TcReplaceJtStringTable.ERROR_INSTANCES_IN_MODELING_MODE_SET : TcReplaceJtStringTable.ERROR_INSTANCES_IN_MODELING_MODE_REPLACE;
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.AppendLine(str1);
      foreach (string str2 in stringList)
        stringBuilder.AppendLine(str2);
      int num = (int) TxMessageBox.ShowModal(stringBuilder.ToString(), TcReplaceJtStringTable.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    return flag;
  }

  public bool PromptSaveOrUpdateTcAndProceed()
  {
    return TxEngineeringDataInternal.GlobalGetStudyFormat().Equals((object) (TxStudyFormatEnum) 1) ? TcCommandsUtility.PromptSaveStudyAndProceed(TcReplaceJtStringTable.SAVE_TEXT, base.Name) : TcCommandsUtility.PromptUpdateTcAndProceed(TcReplaceJtStringTable.UPDATE_TC_TEXT, base.Name);
  }

  private void ExecuteReplace(CApTcReplaceJtModel model)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    TxErrorStack.Clear();
    try
    {
      model.ReplaceJt();
    }
    catch (TxException ex)
    {
      int num = (int) TxMessageBox.Show(!this.IsReplaceMode(model) ? TcReplaceJtStringTable.FAILURE_TO_SET_JT : TcReplaceJtStringTable.FAILURE_TO_REPLACE_JT, TcReplaceJtStringTable.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    finally
    {
      Cursor.Current = current;
      TxErrorStackDlg.DisplayErrors();
    }
  }
}
